package com.project.web.order.controller.app;


import com.project.common.core.utils.exception.CustomException;
import com.project.common.core.utils.exception.RESP_CODE_ENUM;
import com.project.common.core.utils.exception.Result;
import com.project.common.core.utils.redis.RedisClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 控制器编码范例
 *
 * @author abc
 * @date 2019-08-20 19:57
 */
@Api(tags = "编码范例")
@ApiIgnore
@RestController("orderDemoController")
@RequestMapping("/order/auth/demo")
public class DemoController extends com.project.web.order.cßßßontroller.common.BaseController {

    @Resource(name = "redisClient")
    private RedisClient redisClient;

    /**
     * 测试接口
     *
     * @param test 测试参数
     * @return
     */
    @ApiOperation(value = "测试接口")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "test", value = "测试参数", dataType = "String", required = true)
    })
    @GetMapping(value = "/test")
    public Result<String> test(String test) {
        return getResult(this.getClass().getName() + "接口" + test);
    }

    /**
     * 普通控制器接口书写规范
     *
     * @return
     */
    @ApiOperation(value = "普通接口书写范例")
    @RequestMapping(value = "/updateSysConfig", method = RequestMethod.GET)
    public Result<Map<String, Object>> updateSysConfig() {


        // redis存取值
        redisClient.set("key", 123);
        redisClient.get("key");

        // controller层异常方式
        if (false) {
            // 必要的地方打印日志【日志使用占位符方式，如果打印堆栈信息，可以使用ExceptionUtils】
            log.info("此处有错误:{}", "错误信息");
            // 如果需要返回错误提示【框架中错误返回均以全局异常处理】
            throw new CustomException(RESP_CODE_ENUM.ILLEGAL_PARAMETER);
        }

        // 如果返回对象信息，可以为简单类型、复合类型[Object、Map、List、model实体等]
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("a", "a");
        returnMap.put("b", new ArrayList<String>());
        returnMap.put("c", 1);
        return getResult(returnMap);
    }


}
